;(function($){
	$.fn.indexMenuShort = function(opt){
		var el = this;
		var opts = {
			appendDivId:"",				//父级divId				*********必填**********
			menu:{},					//菜单				*********必填**********
            shortcut:"",                //快捷键            
			app:"frontoffice",			//模块				*********必填**********
			homepage_href:"index.html",				//主页URL
			indexOff_href:"html/login/login.html",	//退出URL
			
			
			dataBack : function(){},
			returnBack : function(){},
		}
		var param = $.extend(opts,opt);
		var app = new App(el,param);

		$.indexMenuShort=app;
	}

    var App = function(el,param){
		this.el = el;
		this.param = param;
		this.init();
		this.bindEvent();

	}

    App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
		},
        init : function(){
            var t=this;
			var el = this.el;
			var param = this.param;

            $("#"+param.appendDivId).empty()
			var html = 	'<style>'+
							'#menuQck li{min-width: 69px;height:69px;list-style-type:none;}'+
							'#menuQck li a{text-align: center;width:100% !important;height:100%}'+
                            '#menuQck .btn.btn-app > .ace-icon{line-height:20px;font-size:24px;}'+
                            '#menuQck .btn.btn-app{width:auto;font-size:15px;border-radius:2px !important;background-image:linear-gradient(to bottom, #f4f4f4 0%, #f4f4f4 100%) !important;}'+
							'#menuQck .btn.btn-app:hover{background-image:linear-gradient(to bottom, #DDDDDD 0%, #DDDDDD 100%) !important;}'+
							'#menuQck::-webkit-scrollbar {width: 1px;height:4px}'+
							'#menuQck::-webkit-scrollbar-thumb {border-radius: 10px;-webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);background: rgba(0,0,0,0.2);}'+
							'#menuQck::-webkit-scrollbar-track {-webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);border-radius: 0;background: rgba(0,0,0,0.1);}'+
							'.QCKBtn .dropdown-menu{position:fixed;}'+
							'.QCKBtn .dropdown-menu.dropdown-caret:before{border-top:7px solid rgba(0, 0, 0, 0.2);border-bottom:0;bottom:-7px;top:auto;}'+
							'.QCKBtn .dropdown-menu.dropdown-caret:after{border-top:6px solid #FFF;border-bottom:0;bottom:-6px;top:auto;}'+
						'</style>'+
                        '<div class="nav nav-list" style="width: 100%;position: relative;height: 89px;padding-left: 0;padding-right: 0;border: 0;">'+
				      		'<ul id="menuQck" style="background-color: white;height: 89px;width: 100%;position: absolute;display:flex;margin:0;flex-direction: row;align-items: center;justify-content: space-around;overflow-x: visible;background-color: white;overflow-y: hidden;" class=""></ul>'+
				        '</div>'+
                        '<script type="text/javascript">'+
							'try {ace.settings.check("sidebar", "collapsed")} catch (e) {}'+
						'</script>';
			$("#"+param.appendDivId).append(html)

            t.showMenu(param.menu)
        },
        showMenu:function(menu_json){
			var t=this;
			var el = this.el;
			var param = this.param;

			//生成主页面菜单
			//获取全部部门
			var bmhs = new Promise(t.getBmh);
			//异步获取成功
			bmhs.then((data)=>{
				$.HSAPI_MAINMENU(menu_json,data);

				el.find("#menuQck").empty();
				
				var html_home =	'<li data-menucode="homepage" class="hover QCKBtn ">'+
									'<a class="btn btn-app btn-light" data-menucode="homepage" data-pmenuCode="#/html/homepage/homepage.html"  href="#/html/homepage/homepage.html?&custom=homepage">'+
										'<i class="ace-icon fa fa-home " style=""></i>'+
										'<span class="menu-text">首页</span>'+
									'</a>'+
								'</li>';
							   
				el.find("#menuQck").append(html_home);

				let menu_json_new = []
				for(var item in menu_json){
					menu_json_new = menu_json_new.concat(menu_json[item]);
					menu_json_new.sort((a,b)=>{ return a.sequence-b.sequence})
				}

                var shortcut_arr = param.shortcut.split(",")
				$.each(menu_json_new,function(key,value){
					if( shortcut_arr.indexOf(value.menu_code.trim()) >= 0){
						var color = "#585858"
						if(value.quick_color && value.quick_color!=""){
							color = value.quick_color
						}


						if(value.app.indexOf(param.app)>-1){
							if(value.data_url!="" && value.data_url!="#"){
                                var href = ''
                                if(value.data_url.indexOf('?&') >= 0){
                                    href = '#/'+value.data_url+'&arg='+value.menu_code.trim()+'&pmenucode='+value.parent_menucode+''
                                }else{
                                    href = '#/'+value.data_url+'?&arg='+value.menu_code.trim()+'&pmenucode='+value.parent_menucode+''
                                }
                               
								var html =	'<li data-menucode="'+value.menu_code.trim()+'" class="hover QCKBtn ">'+
												'<a class="btn btn-app btn-light" data-menucode="'+value.menu_code.trim()+'" data-pmenuCode="'+value.parent_menucode+'"  data-url="'+value.data_url+'" href="'+href+'">'+
													'<i class="'+value.menu_class+'" style="color:'+color+'"></i>'+'<span class="menu-text">'+$.i18n.propJsp(value.menu_name)+'</span>'+
												'</a>'+
											'</li>';
								el.find("#menuQck").append(html);
							}else{
								var html =	'<li data-menucode="'+value.menu_code.trim()+'" class="hover QCKBtn inline position-relative  dropdown-hover hover-show hover-shown">'+
												'<a class="btn btn-app btn-light dropdown-toggle" data-menucode="'+value.menu_code.trim()+'" data-pmenuCode="'+value.parent_menucode+'" >'+
													'<i class="'+value.menu_class+'" style="color:'+color+'"></i>'+'<span class="menu-text">'+$.i18n.propJsp(value.menu_name)+'</span>'+
												'</a>'+
												'<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-caret dropdown-close" style="width:238px;" data-menu-id="'+value.menu_code.trim()+'"></ul>'+
											'</li>';
								el.find("#menuQck").append(html);

								var ul=el.find("#menuQck").find("ul[data-menu-id='"+value.menu_code.trim()+"']");
								$(menu_json[value.menu_code.trim()]).each(function(key,value){
									var color = "#585858"
									if(value.quick_color && value.quick_color!=""){
										color = value.quick_color
									}
									if(value.app.indexOf(param.app)>-1){
                                        var href = ''
                                        if(value.data_url.indexOf('?&') >= 0){
                                            href = '#/'+value.data_url+'&arg='+value.menu_code.trim()+'&pmenucode='+value.parent_menucode+''
                                        }else{
                                            href = '#/'+value.data_url+'?&arg='+value.menu_code.trim()+'&pmenucode='+value.parent_menucode+''
                                        }

										var html=	'<li data-menucode="'+value.menu_code.trim()+'" title="'+$.i18n.propJsp(value.menu_name)+'" class="hover" style="width:69px;height:69px;float:left;margin:4px">'+
														'<a class="btn btn-app btn-light" style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;width:100%;padding-left:6px;padding-right:6px;" data-menucode="'+value.menu_code.trim()+'"   data-url="'+value.data_url+'" href="'+href+'">'+
															'<i class="'+value.menu_class+'" style="color:'+color+';width: auto;display: block;"></i>'+
															'<span class="menu-text"  style="">'+$.i18n.propJsp(value.menu_name)+'</span>'+
														'</a>'+
													'</li>';
										
										$(ul).append(html);
									} 
								});
							}
						}
					}
				})

				//调整间距样式
				let width = 0
				$("#menuQck .QCKBtn").each(function(){
					width += $(this).width()
				})
				if(width>=$("#menuQck").width()){
					$("#menuQck").css("justify-content","flex-start")
				}else{
					$("#menuQck").css("justify-content","space-around")
					if(width+($("#menuQck .QCKBtn").length+1) * 10<=$("#menuQck").width()){
						let padding = $("#menuQck").width() - width - ($("#menuQck .QCKBtn").length+1) * 20
						$("#menuQck").css("padding","0px "+padding/2+"px")
					}else{
						$("#menuQck").css("padding","0px")
					}
				}

				el.find("#menuQck").find("a.dropdown-toggle").hover(function(){
					var Y = $(this).offset().left;
					var X = $(this).offset().top;
					var left = 0;
					var ul_width = $(".QCKBtn.hover-show.hover-shown").find("ul.dropdown-menu").width()
					var style_text = ''
					$(this).parent("li").find("style").remove()

					if(Y>=$(window).width()/2){
						if(Y+ul_width/2 >= $(window).width()){
							Y = Y - ul_width + $(this).width()
							left = ul_width*5/6;
							style_text = 	'<style>'+
												'.QCKBtn .dropdown-menu.dropdown-caret:before{left:'+left+'px;}'+
												'.QCKBtn .dropdown-menu.dropdown-caret:after{left:'+(left+1)+'px;}'+
											'</style>';
						}else{
							Y = Y - ul_width/2 + $(this).width()/2
							left = ul_width/2-6;
							style_text = 	'<style>'+
												'.QCKBtn .dropdown-menu.dropdown-caret:before{left:'+left+'px;}'+
												'.QCKBtn .dropdown-menu.dropdown-caret:after{left:'+(left+1)+'px;}'+
											'</style>';
						}
					}else{
						if(Y-ul_width/2 < 0){
							left = Y+$(this).width()/2-6;
							Y = 0;
							style_text = 	'<style>'+
												'.QCKBtn .dropdown-menu.dropdown-caret:before{left:'+left+'px;}'+
												'.QCKBtn .dropdown-menu.dropdown-caret:after{left:'+(left+1)+'px;}'+
											'</style>';
						}else{
							left = ul_width/2-6;
							Y = Y - ul_width/2 + $(this).width()/2;
							style_text = 	'<style>'+
												'.QCKBtn .dropdown-menu.dropdown-caret:before{left:'+left+'px;}'+
												'.QCKBtn .dropdown-menu.dropdown-caret:after{left:'+(left+1)+'px;}'+
											'</style>';
						}
					}
					$(".QCKBtn.hover-show.hover-shown").find("ul.dropdown-menu").css("left",Y+"px").css("bottom","80px").css("top","auto")
					$(this).parent("li").prepend(style_text)
					// debugger
				})

				// el.find("#menuQck a").unbind("click").click(function(){
				// 	debugger
				// 	var url = $(this).attr("href")
				// 	window.open(url)
				// })

			})
			
			
        }, 

		getBmh:function(resolve, reject){
			var where = {};
			var fileds={};
			var wherestring = {}
			fileds = {"fileds":{"fileds":["bh","mc"]}};
			var str=" bh != '000' and bh != '999'"
			wherestring = {"wherestring":["@@WS",str]}
			where = {where:{1:1}}
			var table={};
			table.table={"table":"bm"};				
			c= jQuery.extend({}, where,fileds,table,wherestring); 
			$.ajax({   
				type : 'POST',
				url :$.HSAPI_AJAXURL+"/qms/mobile/procedure2",
				data : $.HSAPI_ASKJSONINFO( JSON.stringify(c),'ExecRetrieveJson','{}'),
				dataType : 'json',  
				success : function(data){ 
					if(data.RETURN == 1){
						resolve(data.DATA);
					}else{
						reject(data.MSG);
					}
				},  
				error : function(data){
					reject(data);
				}  
			});
		}
    }

})(jQuery);